package com.mango.community.service;

import com.mango.community.dao.DiscussPostMapper;
import com.mango.community.entity.DiscussPost;
import com.mango.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @FileName: DiscussPostService
 * @Author:  MangoCookieMilk
 * @Date:     2020/7/19 20:59
 * @Description: DiscussPost service
 * @History:
 */
@Service
public class DiscussPostService {
    @Autowired
    private DiscussPostMapper discussPostMapper;

    /**
     * 注入敏感词过滤工具
     */
    @Autowired
    private SensitiveFilter sensitiveFilter;

    public List<DiscussPost> findDiscussPosts(int userId,int offset,int limit){
        return discussPostMapper.selectDiscussPosts(userId, offset, limit);
    }
    public int findDiscussPostRows(int userId){
        return discussPostMapper.selectDiscussPostRows(userId);
    }

    /**  新增帖子的功能
     * @param discussPost
     * @return
     */
    public int addDiscussPost(DiscussPost discussPost){
        if (discussPost == null){
            throw  new IllegalArgumentException("参数不能为空");
        }
        //转义html标记  将内容中含有html的标签转义为文本
        discussPost.setTitle(HtmlUtils.htmlEscape(discussPost.getTitle()));
        discussPost.setContent(HtmlUtils.htmlEscape(discussPost.getContent()));
        //过滤敏感词
        discussPost.setTitle(sensitiveFilter.filter(discussPost.getTitle()));
        discussPost.setContent(sensitiveFilter.filter(discussPost.getContent()));
        return discussPostMapper.insertDiscussPost(discussPost);
    }

    /** 通过帖子id 查询帖子
     * @param id
     * @return
     */
    public DiscussPost findDiscussPostById(int id){
        return discussPostMapper.selectDiscussPostById(id);
    }

    /** 根据帖子id更新评论数
     * @param id
     * @param CommentCount
     * @return
     */
    public int updateCommentCount(int id ,int CommentCount){
        return discussPostMapper.updateCommentCount(id, CommentCount);
    }
}
